﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DemoTest : MonoBehaviour
{
    public VoiceRecognitionInterface vri;
    public Text txtKeyMatches;
    public Text txtError;
    public Text txtWords;
    public string[] keys;
    public Toggle matchPinyin;
    public Text buttonText;

    bool recognizing = false;

    // Start is called before the first frame update
    void Start()
    {
        vri.Register(gameObject);
        txtWords.text = "";
        txtKeyMatches.text = "";
        for (int i = 0; i < keys.Length; i++)
        {
            txtKeyMatches.text += keys[i] + " ";
            txtKeyMatches.text += "[无]";
            if (i + 1 < keys.Length)
                txtKeyMatches.text += "，";
        }
        txtError.text = "";
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void OnChangeMatchPinyin()
    {
        vri.SetMatchPinyin(matchPinyin.isOn);
    }

    public void Switch()
    {
        if(recognizing)
        {
            recognizing = false;
            StopRecognition();
            buttonText.text = "开始";
        }
        else
        {
            recognizing = true;
            StartRecognition();
            buttonText.text = "结束";
        }
    }

    void StartRecognition()
    {
        vri.VoiceRecognitionInterface_StartRecognition(keys);

        txtWords.text = "";
    }

    void StopRecognition()
    {
        vri.VoiceRecognitionInterface_StopRecognition();
        txtWords.text = "";
        txtKeyMatches.text = "";
        for (int i = 0; i < keys.Length; i++)
        {
            txtKeyMatches.text += keys[i] + " ";
            txtKeyMatches.text += "[无]";
            if (i + 1 < keys.Length)
                txtKeyMatches.text += "，";
        }
    }

    void OnVoiceRecognitionInterface_Result(VoiceRecognitionInterface.Output output)
    {
        txtError.text = "";
        txtWords.text = output.words;
        txtKeyMatches.text = "";
        for(int i=0;i<output.hasKeys.Length;i++)
        {
            txtKeyMatches.text += keys[i] + " ";
            if (output.hasKeys[i])
                txtKeyMatches.text += "[有]";
            else
                txtKeyMatches.text += "[无]";
            if(i+1 < output.hasKeys.Length)
                txtKeyMatches.text += "，";
        }
    }

    void OnVoiceRecognitionInterface_NoResponse()
    {
        txtError.text = "网络不通";
    }
}
